#include <iostream>
#include <cmath>
using namespace std;
long long calc(long long x, long long y, int level, int type)
{ 
    //x,y start from 0, total size:3^level*3^level,type=0-3:起点在左下角，左上角，右上角，右下角
    if (level == 0)
        return 0;
    long long int step, base = pow(9, level - 1);
    if (type == 1)
        y = pow(3, level) - 1 - y;
    else if (type == 2)
        x = pow(3, level) - 1 - x, y = pow(3, level) - 1 - y;
    else if (type == 3)
        x = pow(3, level) - 1 - x;
    if (x < pow(3, level - 1))
    {
        if (y < pow(3, level - 1))
        {
            step = 0 * base;
            step += calc(x, y, level - 1, 0);
        }
        else if (y < pow(3, level - 1) * 2)
        {
            step = 1 * base;
            step += calc(x, y - pow(3, level - 1), level - 1, 3);
        }
        else
        {
            step = 2 * base;
            step += calc(x, y - pow(3, level - 1) * 2, level - 1, 0);
        }
    }
    else if (x < pow(3, level - 1) * 2)
    {
        if (y < pow(3, level - 1))
        {
            step = 5 * base;
            step += calc(x - pow(3, level - 1), y, level - 1, 1);
        }
        else if (y < pow(3, level - 1) * 2)
        {
            step = 4 * base;
            step += calc(x - pow(3, level - 1), y - pow(3, level - 1), level - 1, 2);
        }
        else
        {
            step = 3 * base;
            step += calc(x - pow(3, level - 1), y - pow(3, level - 1) * 2, level - 1, 1);
        }
    }
    else
    {
        if (y < pow(3, level - 1))
        {
            step = 6 * base;
            step += calc(x - 2 * pow(3, level - 1), y, level - 1, 0);
        }
        else if (y < pow(3, level - 1) * 2)
        {
            step = 7 * base;
            step += calc(x - 2 * pow(3, level - 1), y - pow(3, level - 1), level - 1, 3);
        }
        else
        {
            step = 8 * base;
            step += calc(x - 2 * pow(3, level - 1), y - pow(3, level - 1) * 2, level - 1, 0);
        }
    }
    return step;
}
int main()
{
    int level;
    cin >> level;
    long long int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    long long s1 = calc(x1, y1, level, 0);
    //cout<<endl;
    long long s2 = calc(x2, y2, level, 0);
    cout << s2 - s1;
    return 0;
}
